package wstrader.task;

import java.sql.*;
import pc.util.*;

public class Average
{
  public double total = 0;
  public double black = 0;
  public double brown = 0;
  public double fm    = 0;
  public double broken = 0;
  public double moisture = 0;
  public double screen20 = 0;
  public double screen19 = 0;
  public double screen18 = 0;
  public double screen17 = 0;
  public double screen16 = 0;
  public double screen15 = 0;
  public double screen14 = 0;
  public double screen13 = 0;
  public double screen12 = 0;
  public double screen11 = 0;
  public double total_percent = 0;
  private ResultSet rs = null;

  public Average()
  {
  }

  public Average(ResultSet rs,String total_field)
  {
    this.rs = rs;
    try {
      this.total    = rs.getDouble(total_field)*0.001;
      this.black    = rs.getDouble("black")*this.total*0.01;
      this.brown    = rs.getDouble("brown")*this.total*0.01;
      this.fm       = rs.getDouble("foreign_matter")*this.total*0.01;
      this.broken   = rs.getDouble("broken")*this.total*0.01;
      this.moisture = rs.getDouble("moisture")*this.total*0.01;
      this.screen20 = rs.getDouble("screen20")*this.total*0.01;
      this.screen19 = rs.getDouble("screen19")*this.total*0.01;
      this.screen18 = rs.getDouble("screen18")*this.total*0.01;
      this.screen17 = rs.getDouble("screen17")*this.total*0.01;
      this.screen16 = rs.getDouble("screen16")*this.total*0.01;
      this.screen15 = rs.getDouble("screen15")*this.total*0.01;
      this.screen14 = rs.getDouble("screen14")*this.total*0.01;
      this.screen13 = rs.getDouble("screen13")*this.total*0.01;
      this.screen12 = rs.getDouble("screen12")*this.total*0.01;
      this.screen11 = rs.getDouble("screen11")*this.total*0.01;
      for (int i = 11; i <= 20 ; i++) {
        this.total_percent += rs.getDouble("screen" + i);
      }
    } catch (SQLException ex) {
      System.out.println(ex.getMessage());
    }
  }

  public void add(Average av)
  {
    this.total    += av.total;
    this.black    += av.black;
    this.brown    += av.brown;
    this.fm       += av.fm;
    this.broken   += av.broken;
    this.moisture += av.moisture;
    this.screen20 += av.screen20;
    this.screen19 += av.screen19;
    this.screen18 += av.screen18;
    this.screen17 += av.screen17;
    this.screen16 += av.screen16;
    this.screen15 += av.screen15;
    this.screen14 += av.screen14;
    this.screen13 += av.screen13;
    this.screen12 += av.screen12;
    this.screen11 += av.screen11;
  }

  public int printPercent(ExcelFile ex,int x,int y)
  {
    int px = x;
    //ex.zero_blank = false;
    ex.setCellValue(x++, y, this.black*100/this.total);
    ex.setCellValue(x++, y, this.brown*100/this.total);
    ex.setCellValue(x++, y, this.fm*100/this.total);
    ex.setCellValue(x++, y, this.broken*100/this.total);
    ex.setCellValue(x++, y, this.moisture*100/this.total);
    ex.setCellValue(x++, y, this.screen20*100/this.total);
    ex.setCellValue(x++, y, this.screen19*100/this.total);
    ex.setCellValue(x++, y, this.screen18*100/this.total);
    ex.setCellValue(x++, y, this.screen17*100/this.total);
    ex.setCellValue(x++, y, this.screen16*100/this.total);
    ex.setCellValue(x++, y, this.screen15*100/this.total);
    ex.setCellValue(x++, y, this.screen14*100/this.total);
    ex.setCellValue(x++, y, this.screen13*100/this.total);
    ex.setCellValue(x++, y, this.screen12*100/this.total);
    ex.setCellValue(x++, y, this.screen11*100/this.total);
    //ex.zero_blank = true;
    this.total_percent = 0;
    for (int i = px + 5; i < x; i++) {
      double v = ex.getCell(i, y).getNumericCellValue();
      this.total_percent += v;
    }
    while (px < x) ex.setCellStyle(px++,y,"B");

    return x;
  }

  public int printTons(ExcelFile ex,int x,int y)
  {
    int px = x;
    //ex.zero_blank = false;
    ex.setCellValue(x++, y, this.black);
    ex.setCellValue(x++, y, this.brown);
    ex.setCellValue(x++, y, this.fm);
    ex.setCellValue(x++, y, this.broken);
    ex.setCellValue(x++, y, this.moisture);
    ex.setCellValue(x++, y, this.screen20);
    ex.setCellValue(x++, y, this.screen19);
    ex.setCellValue(x++, y, this.screen18);
    ex.setCellValue(x++, y, this.screen17);
    ex.setCellValue(x++, y, this.screen16);
    ex.setCellValue(x++, y, this.screen15);
    ex.setCellValue(x++, y, this.screen14);
    ex.setCellValue(x++, y, this.screen13);
    ex.setCellValue(x++, y, this.screen12);
    ex.setCellValue(x++, y, this.screen11);
    //ex.zero_blank = true;

    while (px < x) ex.setCellStyle(px++,y,"B");

    return x;
  }

  public int printQR(ExcelFile ex, int x,int y)
  {
    if (rs == null) return x;
    //ex.zero_blank = false;
    try {
      ex.setCellValue(x++, y, rs.getDouble("black"));
      ex.setCellValue(x++, y, rs.getDouble("brown"));
      ex.setCellValue(x++, y, rs.getDouble("foreign_matter"));
      ex.setCellValue(x++, y, rs.getDouble("broken"));
      ex.setCellValue(x++, y, rs.getDouble("moisture"));
      ex.setCellValue(x++, y, rs.getDouble("screen20"));
      ex.setCellValue(x++, y, rs.getDouble("screen19"));
      ex.setCellValue(x++, y, rs.getDouble("screen18"));
      ex.setCellValue(x++, y, rs.getDouble("screen17"));
      ex.setCellValue(x++, y, rs.getDouble("screen16"));
      ex.setCellValue(x++, y, rs.getDouble("screen15"));
      ex.setCellValue(x++, y, rs.getDouble("screen14"));
      ex.setCellValue(x++, y, rs.getDouble("screen13"));
      ex.setCellValue(x++, y, rs.getDouble("screen12"));
      ex.setCellValue(x++, y, rs.getDouble("screen11"));
    }
    catch (SQLException e) {
      e.printStackTrace();
    }
    //ex.zero_blank = true;
    return x;
  }

}

